package ${package}.web.controller.${rootPath};

import ${package}.entity.${className};
import ${package}.service.${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ${package}.viewmodel.admin.ResultVM;
import org.springframework.context.ApplicationEventPublisher;
import java.util.List;
import org.springframework.data.domain.Page;

<#import "CopyRight.ftl" as my>
<@my.CopyRight/>
@RequestMapping("/api/${className?uncap_first}")
@RestController
public class ${className}Ctrl {

    @Autowired
    private ${className}Service ${className?uncap_first}Service;

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @GetMapping
    public ResultVM<Page> list(Integer pageNo, Integer pageSize)
    {
        Page result = ${className?uncap_first}Service.page(pageNo,pageSize);
        eventPublisher.publishEvent(new UserEvent(this,"分页查询${entityRemark}"));
        return ResultVM.success(result);
    }


    @GetMapping("/{id}")
    public ResultVM<${className}> queryById(@PathVariable("id") Integer id)
    {
        ${className} ${className?uncap_first} = ${className?uncap_first}Service.findById(id);
        eventPublisher.publishEvent(new UserEvent(this,"查询${entityRemark}详情"+id));
        return ResultVM.success(${className?uncap_first});
    }


    @GetMapping("/all")
    public ResultVM<List<${className}>> getAll()
    {
        List<${className}> result = ${className?uncap_first}Service.findAll();
        eventPublisher.publishEvent(new UserEvent(this,"查询所有${entityRemark}"));
        return ResultVM.success(result);
    }

    @DeleteMapping
    public ResultVM<Integer> delete(@RequestBody Integer[] ids)
    {
        ${className?uncap_first}Service.deleteByIds(ids);
        eventPublisher.publishEvent(new UserEvent(this,"删除${entityRemark}"+ Arrays.toString(ids)));
        return ResultVM.success(null);
    }

    @PutMapping
    public ResultVM<Integer> update(@RequestBody ${className} ${className?uncap_first})
    {
        ${className?uncap_first}Service.update(${className?uncap_first});
        eventPublisher.publishEvent(new UserEvent(this,"更新${entityRemark}"+ ${className?uncap_first}.getId()));
        return ResultVM.success(null);
    }

    @PostMapping
    public ResultVM<Integer> add(@RequestBody ${className} ${className?uncap_first})
    {
        ${className?uncap_first}Service.save(${className?uncap_first});
        eventPublisher.publishEvent(new UserEvent(this,"新增${entityRemark}"+ ${className?uncap_first}.getId()));
        return ResultVM.success(null);
    }
}
